package one.stand.service.impl;

import com.google.common.collect.Lists;
import one.stand.code.RedisCode;
import one.stand.enums.LoginEnum;
import one.stand.service.MessageMsgService;
import one.stand.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author cjr
 */
@Service
public class MessageMsgServiceImpl implements MessageMsgService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void updateUnreadNum(String targetId, String sendId, String targetLoginProgramName) {
        //
        String unreadNumKey = RedisCode.getUnreadNum(targetId, sendId, targetLoginProgramName);
        stringRedisTemplate.opsForValue().increment(unreadNumKey, 1);

        //notify
        String unreadNotifyKey = RedisCode.getUnreadNotify(targetId, targetLoginProgramName);
        stringRedisTemplate.opsForValue().increment(unreadNotifyKey, 1);
    }

    @Override
    public void removeUnreadNum(String targetId, String sendId, String targetLoginProgramName) {
        String unreadNumKey = RedisCode.getUnreadNum(targetId, sendId, targetLoginProgramName);
        redisUtil.remove(unreadNumKey);

        Integer num = getUnreadNumAll(targetId, targetLoginProgramName);
        String unreadNotifyKey = RedisCode.getUnreadNotify(targetId, targetLoginProgramName);
        if (Objects.isNull(num) || num <= 0) {
            redisUtil.remove(unreadNotifyKey);
        }
    }

    @Override
    public void removeUnreadNumAll(String targetId, String targetLoginProgramName) {
        String key = RedisCode.getUnreadNum(targetId, "*", targetLoginProgramName);
        redisUtil.removeLike(key);

        String unreadNotifyKey = RedisCode.getUnreadNotify(targetId, targetLoginProgramName);
        redisUtil.remove(unreadNotifyKey);
    }

    @Override
    public Integer getUnreadNum(String targetId, String sendId, String targetLoginProgramName) {
        Integer num = redisUtil.get(RedisCode.getUnreadNum(targetId, sendId, targetLoginProgramName));
        if (num == null) {
            num = 0;
        }
        return num;
    }

    @Override
    public Integer getUnreadNumAll(String targetId, String targetLoginProgramName) {
        Set<String> keys = redisUtil.getKey(RedisCode.getUnreadNum(targetId, "*", targetLoginProgramName));
        Integer total = 0;
        Integer num;
        for (String v : keys) {
            num = redisUtil.get(v);
            if (num == null) {
                num = 0;
            }
            total = total + num;
        }
        return total;
    }

    @Override
    public List<String> getUnreadNotify() {
        Set<String> keys = redisUtil.getKey(RedisCode.getUnreadNotify("*", LoginEnum.CRM.getCode()));
        if (Objects.isNull(keys) || keys.size() <= 0) return null;
        List<String> targetIds = Lists.newArrayListWithExpectedSize(keys.size());
        for (String key : keys) {
            targetIds.add(StringUtils.substringAfterLast(key, RedisCode.SYMBOL));
        }
        redisUtil.batchDel(keys);// delete keys
        return targetIds;
    }
}
